package com.xiaoshi.system.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoshi.system.dto.SysRoleDto;
import com.xiaoshi.system.entity.SysRole;
import com.xiaoshi.system.service.ISysRoleService;
import com.xiaoshi.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author yuankai
 * @since 2022-03-21
 */
@RestController
@Api(tags = "角色管理")
@RequestMapping("/system/sys-role")
public class SysRoleController {

    @Autowired
    private ISysRoleService roleService;

    @ApiOperation("获取角色分页信息")
    @GetMapping("/getPage")
    public JsonResult getPage(Page page, String roleName){
        return JsonResult.success(roleService.getPage(page,roleName));
    }

    @ApiOperation("获取角色List")
    @GetMapping("/getList")
    public JsonResult getList(){
        return JsonResult.success(roleService.getList());
    }

    @ApiOperation("根据ID删除角色")
    @GetMapping("/delRoleById")
    public JsonResult delRoleById(Long roleId){
        if(roleService.delRoleById(roleId)){
            return JsonResult.success();
        }
        return JsonResult.fail();
    }

    /**
     * 添加角色信息
     * @param dto
     * @param bindingResult
     * @return
     */
    @ApiOperation("添加角色")
    @PostMapping("/saveRole")
    public JsonResult saveRole(@Validated @RequestBody SysRoleDto dto, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return JsonResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        if(roleService.saveRole(dto)){
            return JsonResult.success();
        }
        return JsonResult.fail();
    }
    /**
     * 修改角色信息
     * @param sysRole
     * @return
     */
    @ApiOperation("修改角色")
    @PostMapping("/updateRole")
    public JsonResult updateRole(SysRole sysRole){

        if(roleService.updateRole(sysRole)){
            return JsonResult.success();
        }
        return JsonResult.fail();
    }
}
